#!/usr/bin/env python
# -*- encoding: utf8 -*-

import os
import sys
import random
import datetime
import settings
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
sys.path.append('../')
from reps.interns.models import *

print 'Moving old DB to "reps.db.old"'
try:
    os.rename('reps.db', 'reps.db.old')
except OSError:
    print 'Old DB not found'

print 'Running "python manage.py syncdb --noinput"'
print '-' * 70
os.system('python manage.py syncdb --noinput')
print '-' * 70

print 'Creating superuser (login=admin, password=admin)'
admin = User.objects.create_user('admin', 'admin@example.com', 'admin')
admin.is_staff = True
admin.is_superuser = True
admin.save()


print 'Creating test data'

print ' Faculty'
faculty = Faculty.objects.create(full_name='Elektrotechniki, Automatyki, Informatyki i Elektroniki', short_name='EAIiE')

print ' Teacher'
teacher_user = User.objects.create_user('teacher', 'teacher@example.com', 'test')
teacher_user.first_name = 'Nauczyciel'
teacher_user.last_name = 'Nauczycielski'
teacher_user.is_staff = True
teacher_user.is_superuser = False
add_profile(teacher_user)
teacher_user.save()
teacher = Teacher.objects.create(degree='mgr', phone='123456', address='siakiś adres', profile=teacher_user.get_profile(), faculty=faculty)

print ' Specialization'
inf = Specialization.objects.create(full_name='Informatyka', faculty=faculty, teacher=teacher)
infs = Specialization.objects.create(full_name='Informatyka Stosowana', faculty=faculty, teacher=teacher)
el = Specialization.objects.create(full_name='Elektronika', faculty=faculty, teacher=teacher)
et = Specialization.objects.create(full_name='Elektrotechnika', faculty=faculty, teacher=teacher)
auto = Specialization.objects.create(full_name='Automatyka', faculty=faculty, teacher=teacher)
specializations = [inf, infs, el, et, auto]

'''print ' Student'
student_users = []
students = []
for i in xrange(10):
    student_users += [User.objects.create_user('student%d' % i, 'student%d@example.com' % i, 'test')]
    student_users[i].first_name = 'Student%d' % i
    student_users[i].last_name = 'Studentowski'
    add_profile(student_users[i])
    student_users[i].save()
    students += [Student.objects.create(book_no=i * 100,
                                        specialization=specializations[i % len(specializations)],
                                        year_of_study=3 + i % 2,
                                        profile=student_users[i].get_profile())]
# students with multiple specializations
students2 = []
for i in xrange(10):
    student_user = User.objects.create_user('studenti%d' % i, 'student%d@example.com' % i, 'test')
    student_user.first_name = 'Student%d' % i
    student_user.last_name = 'Inny'
    add_profile(student_user)
    student_user.save()
    students2 += [Student.objects.create(book_no=i * 100,
                                         specialization=specializations[i % len(specializations)],
                                         year_of_study=3 + i % 2,
                                         profile=student_user.get_profile())]
    students2 += [Student.objects.create(book_no=i * 100,
                                         specialization=specializations[(i + 1) % len(specializations)],
                                         year_of_study=3 + i % 2,
                                         profile=student_user.get_profile())]
# unregistered students
ur_student_users = []
ur_students = []
for i in xrange(30):
    book_no = i * 100 + 10000
    ur_student_users += [User.objects.create_user(str(book_no), '', str(book_no))]
    ur_student_users[i].is_active = False
    ur_student_users[i].first_name = 'Student%d' % i
    ur_student_users[i].last_name = 'Niezarejestrowany'
    add_profile(ur_student_users[i])
    ur_student_users[i].save()
    ur_students += [Student.objects.create(book_no=book_no,
                                           specialization=specializations[i % len(specializations)],
                                           year_of_study=3 + i % 2,
                                           profile=ur_student_users[i].get_profile())]
'''
print ' Dean'
dean_user = User.objects.create_user('dean', 'dean@example.com', 'test')
dean_user.first_name = 'Jan'
dean_user.last_name = 'Dziekan'
add_profile(dean_user)
dean_user.save()
dean = Dean.objects.create(profile=dean_user.get_profile(),
                           faculty=faculty)

print ' Branch'
main_branches = []
for i in xrange(10):
    main_branches += [Branch.objects.create(main_branch=None,
                                            name='Firma %da' % i,
                                            street='Firmowa',
                                            postal_code='%d%d-%d%d%d' % ((i,) * 5),
                                            city='Firmowo',
                                            created_by=teacher_user),
                      Branch.objects.create(main_branch=None,
                                            name='Firma %db' % i,
                                            street='Firmowa',
                                            postal_code='%d%d-%d%d%d' % ((i,) * 5),
                                            city='Firmowo',
                                            created_by=teacher_user)]
branches = []
for i in xrange(20):
    branches += [Branch.objects.create(main_branch=main_branches[i / 2],
                                       name='Firma %d %d' % (i, i),
                                       street='Firmowa',
                                       postal_code='%d%d-%d%d%d' % ((i % 10,) * 5),
                                       city='Firmowo',
                                       created_by=teacher_user)]

print ' Training_Insurance'
insurances = [Training_Insurance.objects.create(name='Firma ubezpieczeniowa %d' % i) for i in xrange(10)]

print ' Training_Date'
today = datetime.date.today().toordinal()
training_dates = [Training_Date.objects.create(
                    specialization=spec,
                    start_date=datetime.date.fromordinal(today + i),
                    end_date=datetime.date.fromordinal(today + i + 21))
              for i in xrange(10) for spec in specializations]

'''
print ' Training (change pending!)'
trainings = [Training.objects.create(student=random.choice(students),
                                     street=random.choice(["Taka", "Inna", "Pierwsza", "Druga", "Siaka", "Nijaka"]),
                                     postal_code='12-345',
                                     city = random.choice(['Kraków', 'Gdynia', 'Sopot', 'Manieczki']),
                                     branch=random.choice(branches),
                                     training_date=random.choice(training_dates),
                                     training_insurance=random.choice(insurances),
                                     hostel=random.choice([True, False]),
                                     payable=random.choice([True, False]),
                                     #abroad=random.choice([True, False]),
                                     contract_date=datetime.date.fromordinal(today + random.randint(10, 30)),
                                     pass_date=random.choice([None, datetime.date.fromordinal(today + random.randint(40, 50))]),
                                     remarks='uwagi %d' % i,
                                     locked=random.choice([True, False]),
                                     # Change pending
                                     default_insurance=random.choice([True, False]),
                                     insurance_company='Firma ubezpieczeniowa %d' % i,
                                     insurance_number=i,
                                     # /Change pending
                                     dont_print_contract=random.choice([True, False]),
                                     mark=random.choice([None] + Training.MARKS),
                                     is_working=random.choice([True, False]),
                                     is_foreigner=i % 2)
             for i in xrange(20)]
'''

print ' Setting'
Setting(key='default_insurance_company', value='1').save()
Setting(key='default_insurance', value='bla bla bla').save()
Setting(key='email_address', value='user@example.com').save()
#Setting(key='email_user', value='user').save()
#Setting(key='email_password', value='pass').save()

